// pages/train/score/score.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CDNPATH: app.globalData.CDNPATH,
    type: '', // train-演练,exam-通关
    practiceId: '',
    loading: false,
    score: '',
    feedbackShow: false,
    feedbackList: [
      {
        values: [ '不实用', '实用' ],
        checked: ''
      },
      {
        values: [ '太书面', '接地气' ],
        checked: ''
      },
      {
        values: [ '老套陈旧', '与时俱进' ],
        checked: ''
      },
      {
        values: [ '难以掌握', '易学易会' ],
        checked: ''
      },
      {
        values: [ '对话太短', '对话太长' ],
        checked: ''
      }
    ],
    feedbackInfo: {
      firstClassificationIdList: [],
      secondClassificationIdList: [],
      description: ''
    },
    coachingContentId: '',
    chapterId: '',
    trainingLibraryId: '',
    isAdmin: '',
    urlParams: ''
    // score: 'pass'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { practiceId, type, sceneId, sceneName, userClassify,isRecommended, customerSex, coachingContentId = '', chapterId = '', trainingLibraryId = '' } = options
    let code = ''
    if (coachingContentId && coachingContentId !== 'undefined') {
      // 培训
      code = '05-02'
    } else if (chapterId && chapterId !== 'undefined') {
      // 学习
      code = '01-02'
    } else if (trainingLibraryId && trainingLibraryId !== 'undefined') {
      // 训练库非主流程
      code = '02-01'
    } else {
      // 主流程
      code = '02-00'
    }
    const urlParams = `sceneId=${sceneId}&sensors_ext=${code}&sceneName=${sceneName}&userClassify=${userClassify}&isRecommended=${isRecommended}&customerSex=${customerSex}&coachingContentId=${coachingContentId}&chapterId=${chapterId}&trainingLibraryId=${trainingLibraryId}`
    wx.setNavigationBarTitle({
      title: type === 'train' ? '演练' : '通关',
    })
    this.setData({
      practiceId,
      type,
      sceneId,
      sceneName,
      userClassify,
      customerSex,
      coachingContentId,
      chapterId,
      trainingLibraryId,
      isAdmin: app.globalData.userInfo.roleType === 'student-student',
      urlParams,
    })
    app.api.post({
      url: 'feedbackClassification_page',
      data: {
        classificationName: '智能陪练',
        currentPage: 1,
        pageSize: 10
      }
    }).then(res => {
      if (res.list.length) {
        const classification = res.list[0]
        let { feedbackInfo } = this.data
        if (classification.parentId === '0') {
          feedbackInfo.firstClassificationIdList.push(classification.id)
        } else {
          feedbackInfo.firstClassificationIdList.push(classification.parentId)
          feedbackInfo.secondClassificationIdList.push(classification.id)
        }
        this.setData({ feedbackInfo })
      } else {
        
      }
    })
  },
  feedbackOpt(){
    console.log('feedbackOpt')
    this.toggleFeedback()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tryAgain () {
    const { urlParams, type } = this.data
    if (type === 'train') {
      // 演练评分
      wx.redirectTo({
        url: `/pages/train/only-train/onlyTrain?${urlParams}`
      })
    } else {
      // 通关评分
      wx.redirectTo({
        url: `/pages/train/exam/exam?${urlParams}`
      })
    }
    
  },
  scoreChange (e) {
    const { score } = e.detail
    this.setData({
      score,
    })
  },
  next: function () {
    const { urlParams, type } = this.data
    if (type === 'train') {
      // 演练评分下一步
      wx.redirectTo({
        url: `/pages/train/exam/exam?${urlParams}`
      })
    }
  },
  finish () {
    // 完成后跳转回列表页, 所以判断打开过的路由
    const pages = getCurrentPages()
    const backRoutes = [
      'pages/train/learning-link/learningLink', // 学习环节
      'echarts/pages/train/score/score', // 评分
      'pages/train/only-train/onlyTrain', // 演练
      'pages/train/exam/exam', // 通关
      'pages/learn/learn-pdf/learnPdf', // 学习pdf
      'pages/home-page/to-demonstration/toDemonstration', // 现场演示
    ]
    console.log(pages)
    let delta = 0
    for (let i = pages.length - 1; i > 0; i--) {
      if (backRoutes.indexOf(pages[i].route) >= 0) {
        delta++
      } else {
        break
      }
    }
    wx.navigateBack({ delta })
  },
  toggleFeedback () {
    this.setData({
      feedbackShow: !this.data.feedbackShow,
    })
  },
  chooseFeedback (e) {
    const { index, val } = e.currentTarget.dataset
    const { feedbackList } = this.data
    let checked = val
    if (feedbackList[index].checked === val) {
      checked = ''
    }
    this.setData({
      [`feedbackList[${index}].checked`]: checked
    })
  },
  submitFeedback () {
    const { feedbackList, feedbackInfo } = this.data
    let words = feedbackList.map(item => {
      return item.checked
    })
    words = words.filter(val => val)
    if (words.length) {
      feedbackInfo.description = words.join('，')
      feedbackInfo.description = feedbackInfo.description + '。'
      console.log(feedbackInfo.description)
      app.api.post({
        url: 'vx_feedback_createFeedback',
        data: feedbackInfo
      }).then(res => {
        wx.showToast({
          title: '评价成功',
          icon: 'none',
          duration: 2000
        })
        this.setData({
          feedbackList: feedbackList.map(item => {
            item.checked = ''
            return item
          }),
        })
        this.toggleFeedback()
      })
    } else {
      wx.showToast({
        title: '请选择反馈内容',
        icon: 'none',
        duration: 2000
      })
    }
  },
})